#include<iostream>
#include<string>
using namespace std;
#define FRE 0
#define PRE 1
#define OCP 2
#define EXP 3

const int maxn = 10000+10;
struct IP {
  string owner;
  int status;
  int te;
}ip[maxn];
string h;
int N, t_def, t_max, t_min, t_now;
int m;

void update_ip() {
  for(int i=1; i<=N; ++i) {
    switch(ip[i].status) {
      case FRE:
      break;
      case PRE:
        if(t_now >= ip[i].te) {
          ip[i].te = 0;
          ip[i].owner = "";
          ip[i].status = FRE;
        }
      break;
      case OCP:
        if(t_now >= ip[i].te) {
          ip[i].te = 0;
          // ip[i].owner = "";
          ip[i].status = EXP;
        }
      break;
      case EXP:
      break;
    }
  }
}

int get_ip_by_owner(const string &client) {
  for(int i=1; i<=N; ++i) {
    if(ip[i].owner == client) return i;
  }
  return 0;
}

int get_ip_by_type(int type) {
  for(int i=1; i<=N; ++i)
    if(ip[i].status == type) return i;
  return 0;
}


int main() {
  // freopen("in.txt", "r", stdin);
  cin>>N>>t_def>>t_max>>t_min>>h;
  cin>>m;
  while(m--) {
    string client, server, type;
    int ipaddr, te, now;
    cin>>now>>client>>server>>type>>ipaddr>>te;
    t_now = now;
    update_ip();

    if(server!=h && server!="*") {
      if(type!="REQ") continue;
    }
    if(type!="DIS" && type!="REQ") continue;
    if(server=="*"&&type!="DIS" || server==h&&type=="DIS")continue;

    if(type=="DIS") {
      int k = get_ip_by_owner(client);
      if(!k) k = get_ip_by_type(FRE);
      if(!k) k = get_ip_by_type(EXP);
      if(!k) continue;

      ip[k].status = PRE;
      ip[k].owner = client;
      if(!te) te = t_now+t_def;
      else {
        te = max(te, t_now+t_min); te = min(te, t_now+t_max);
      }
      ip[k].te = te;
      cout<<h<<' '<<client<<' '<<"OFR"<<' '<<k<<' '<<te<<'\n';
    }
    else if(type=="REQ") {
      if(server!=h) {
        for(int i=1; i<=N; ++i) {
          if(ip[i].owner == client) {
            if(ip[i].status==PRE) {
              ip[i].status = FRE;
              ip[i].owner = "";
              ip[i].te = 0;
            }
          }
        }
        continue;
      }
      if(ipaddr > N || ip[ipaddr].owner != client) { // 这个下标检查我意识到了但没有写，没想到在这里RE了
        // send NAK
        cout<<server<<' '<<client<<' '<<"NAK"<<' '<<ipaddr<<' '<<0<<'\n';
        continue;
      }
      // ip[ipaddr].owner  = client;
      ip[ipaddr].status = OCP;
      if(!te) te = t_now+t_def;
      else {
        te = max(te, t_now+t_min); te = min(te, t_now+t_max);
      }
      ip[ipaddr].te = te;
      // send ACK
      cout<<server<<' '<<client<<' '<<"ACK"<<' '<<ipaddr<<' '<<te<<'\n';
    }
    else {
      throw("UNKNOWN type: "+type);
    }
  }
}